package com.lee.senior;

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

import java.io.IOException;

public class MyReduce{

    public static class TotalReduce extends Reducer<Text, IntWritable, Text, IntWritable> {

        @Override
        protected void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {

            int total = 0;
            for (IntWritable t : values){
                total += t.get();
            }

            context.write(key, new IntWritable(total));
        }
    }

    public static class SaleSortCombiner extends Reducer<Text, IntWritable, IntWritable, Text> {

        @Override
        protected void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {
            int total = 0;
            for (IntWritable t : values){
                System.out.println("=============" + key.toString() + ":" + t.get());
                total += t.get();
            }

            context.write(new IntWritable(total), key);
        }
    }


    public static class SaleSortReduce extends Reducer<IntWritable, Text, IntWritable, Text> {

        @Override
        protected void reduce(IntWritable key, Iterable<Text> values, Context context) throws IOException, InterruptedException {
            for (Text t : values){
                context.write(key, t);
            }
        }
    }

}
